package cn.com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;

public class TestUpdate {
    public static void main(String[] args) throws Exception {
        //1.反射注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.驱动管理器获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cgb2105 ?characterEncoding=utf8","root","root");
       //3.生成传输器
        Statement st = conn.createStatement();

          /*executeUpdate(插入数据)*/
//        String sql ="insert into dept values(null,'Java开饭','北京') ";
//        int i = st.executeUpdate(sql);
//        System.out.println("数据插入成功");
         /*executeUpdate(删除数据)*/
    //    st.executeUpdate("delete from dept where deptno=6");
         /*executeUpdate(修改该数据)*/
//        st.executeUpdate("update dept set dname='PHP' where deptno=5 ");
//        st.executeUpdate("update dept set loc='上海',dname='IOS' where deptno=4 ");
        /*executeUpdate(创建新的user表),仅次创建一次，不可重复执行此创建*/
//        st.executeUpdate("create table user(id int,name varchar(20),password varchar(10))");
//        System.out.println("创建表成功！！");

        st.executeUpdate("insert into user values(1,'小白','123456')");//执行插入语句后，需要执行查询查看是否成功?

        //遍历结果集
         ResultSet rs = st.executeQuery("select * from dept"); // 查看/查询部门表所有
        while(rs.next()){
            for (int j = 1; j <=3 ; j++) {
                System.out.println(rs.getString(j));
            }

        }

         st.close();
         conn.close();
            rs.close();
    }
}


